<?php defined('SYSPATH') or die('No direct script access.');

/**
 * @file feerule.php
 * @brief 
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-04-05
 */


class Controller_Feerule extends Controller_Rest {
	public function before()
   	{
		parent::before();
	}
	/**
	 * @brief 
	 *
	 * @param $id
	 *
	 * @return $this 
	 */
	public function action_get($id = NULL) 
	{
		if($id === NULL) {
			$this->get_feerules();
		}
		else {
			$this->get_feerule($id);
		}
	}
	
	/**
	 * @brief 
	 *
	 * @param $id
	 *
	 * @return $this
	 */
	private function get_feerule($id) 
	{
		$feerule = ORM::factory('feerule', $id);
		if(!$feerule->id) {
			$this->response->body("feerule not found!")->status(206);
			return;
		}
		$ret_feerule = json_encode($feerule->as_array());
		$this->response->body($ret_feerule)->status(200);
	}
	
	/**
	 * @brief 获取多条feerule 
	 *
	 * 
	 * @return	$this 
	 */
	private function get_feerules() 
	{
		/** 获取起止条数*/
		$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT);
		$pos = filter_input(INPUT_GET, 'pos', FILTER_VALIDATE_INT);

		$feerule = ORM::factory('feerule');
		$bank_id = filter_input(INPUT_GET, 'bank_id', FILTER_VALIDATE_INT);
		$cardtype_id = filter_input(INPUT_GET, 'cardtype_id', FILTER_VALIDATE_INT);
		$grade_id   = filter_input(INPUT_GET, 'grade_id', FILTER_VALIDATE_INT);
		$currency_id= filter_input(INPUT_GET, 'currency_id', FILTER_VALIDATE_INT);
		
		//各个参数为指定或全部，1为全部
		$bank_id = ($bank_id) ? $bank_id : 1;
		$cardtype_id = ($cardtype_id) ? $cardtype_id : 1;
		$grade_id = ($grade_id) ? $grade_id : 1;
		$currency_id = ($currency_id) ? $currency_id : 1;
		

		/** 获取指定银行(if have)指定bank_id, cardtype_id, grade_id, currency_id (if have) 指定起止位置的所有类别条数*/
		
		$total_count = $feerule
					   ->where('bank_id', '=', $bank_id)
					   ->and_where('cardtype_id', '=', $cardtype_id)
					   ->and_where('grade_id', '=', $grade_id)
					   ->and_where('currency_id', '=', $currency_id)
					   ->count_all();
		$start = ($start) ? $start : 0;
		$pos = ($pos) ? $pos :$total_count;
		$feerules = $feerule
			->where('bank_id', '=', $bank_id)
			->and_where('cardtype_id', '=', $cardtype_id)
			->and_where('grade_id', '=', $grade_id)
			->and_where('currency_id', '=', $currency_id)
			->offset($start)
			->limit($pos)
			->find_all();
		
		
		if(!$feerules -> count()) 
		{
			$this->response->body("feerule not exists!")->status(206);	
		}
		else
		{
			$rset['num'] = $feerules->count();
			$rset['list'] = array();
			foreach($feerules as $pri)
			{
				$rset['list'][] = $pri->as_array();
			}
			$json_ret = json_encode($rset);
			$this->response->body($json_ret)->status(200);
		}
	}
	
	/**
	 * @brief 新增feerule
	 *
	 * @return 
	 */
	public function action_post() 
	{
		$feerule = ORM::factory('feerule');
		if(!$_POST) return;
		if(isset($_POST['id']))
		{
			unset($_POST['id']);
		}
		try 
		{
			/** 提交feerule*/
			$feerule->values($_POST)->check();
			$feerule->save();
			$this->response->body('添加feerule成功')->status(200);
		}
		catch (ORM_Validation_Exception $e)
		{
			$error = $e->errors();
			foreach($error as $k=>$v)
			{
				$this->response->body($k.':not available')->status(202);
			}
		}
	}
	/**
	 * @brief 更新指定feerule
	 *
	 * @param $id
	 *
	 * @return $this 
	 */
	public function action_put($id) 
	{
		$feerule = ORM::factory('feerule', $id);
		if(!$feerule->id)
		{
			$this->response->body('feerule不存在！')->status(206);
			return;
		}
		try 
		{
			$feerule->values($this->request->post())->check()->update();
			$this->response->body('更新feerule成功！')->status(200);
		}
		catch (ORM_Validation_Exception $e)
		{/** 验证错误处理*/
			$error = $e->errors();
			foreach($error as $k=>$v)
			{
				$this->response->body($k.':not avalilable')->status(206);
			}
		}
		catch(Exception $err)
		{/** 其他错误处理*/
			$this->response->body($err->getMessage())->status(500);
		}
	}
	/**
	 * @brief 删除指定feerule
	 *
	 * @param $id
	 *
	 * @return $this
	 */
	public function action_delete($id) 
	{
		$feerule = ORM::factory('feerule', $id);
		if(!$feerule->id)
		{
			$this->response->body('feerule不存在！')->status(206);
		}
		$feerule->delete();
		$this->response->body('删除feerule成功！')->status(200);
	}
}
